package demos.executable;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.Vector;

public class Executable extends JFrame implements Runnable {
    private final JList<Object> list = new JList<>();
    private final Vector<Object> vector = new Vector<>();

    public Executable(){
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        getContentPane().add(new JScrollPane(list));
        pack();
        new Thread(this).start();
    }

    public static void main(String... args){
        new Executable();
    }

    @Override
    public void run() {
        setVisible(true);
        try {
            final URL resource = getClass().getResource("images/bulb.gif");
            add(resource);
            BufferedImage image = ImageIO.read(resource);
            add(image);
            TrayIcon trayIcon = new TrayIcon(image);
            add(trayIcon);
            SystemTray systemTray = SystemTray.getSystemTray();
            add(systemTray);
            systemTray.add(trayIcon);
            add("Hurray");
        } catch (IOException | AWTException e) {
            add(e.getMessage());
        } catch (Exception e) {
            add(e.getClass().getSimpleName(), e.getMessage());
        }
    }

    private void add(Object... objects) {
        for(Object o : objects)
            if (o != null)
                vector.add(o.getClass().getSimpleName() + ": " + o);
            else
                vector.add("NULL");
        list.setListData(vector);
    }
}
